﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using Think9.Models;
using Think9.Repository;

namespace Think9.Services.Base
{
    public class ComService
    {
        public ComRepository comRepository = new ComRepository();

        /// <summary>
        /// 获得数据表包含字段列表
        /// </summary>
        public DataTable GetPageList(string tbName, string files, string where, string orderby, int pageIndex, int pageSize, object param)
        {
            return comRepository.GetPageList(tbName, files, where, orderby, pageIndex, pageSize, param);
        }

        /// <summary>
        /// 获得数据表包含字段列表
        /// </summary>
        public DataTable GetTbFieldList(string tbId)
        {
            return comRepository.GetTbFieldList(tbId);
        }

        /// <summary>
        /// 获得已定义的所有视图
        /// </summary>
        public DataTable GetViewList()
        {
            return comRepository.GetViewList();
        }

        /// <summary>
        ///   求值 可以是正常的sql 也可以是算术表达式 , object param
        /// </summary>
        public string GetSingle(string sql)
        {
            return comRepository.GetSingle(sql);
        }

        /// <summary>
        ///   求值 可以是正常的sql 也可以是算术表达式
        /// </summary>
        public string GetSingle(string sql, object param)
        {
            return comRepository.GetSingle(sql, param);
        }

        /// <summary>
        ///  得到最大值
        /// </summary>
        public int GetMaxID(string fieldName, string tbName, string where)
        {
            return comRepository.GetMaxID(fieldName, tbName, where);
        }

        /// <summary>
        ///  返回自动增长列id
        /// </summary>
        public long InsertAndReturnID(string tbName, List<string> columns, object param)
        {
            return comRepository.InsertAndReturnID(tbName, columns, param);
        }

        /// <summary>
        ///  返回自动增长列id
        /// </summary>
        public void Insert(string tbName, List<string> columns, object param)
        {
            comRepository.Insert(tbName, columns, param);
        }

        /// <summary>
        /// 判断是否存在某表的某个字段
        /// </summary>
        public bool ColumnExists(string tbName, string columnName)
        {
            return comRepository.ColumnExists(tbName, columnName);
        }

        /// <summary>
        /// 获得数据表包含字段的字符串以#分割
        /// </summary>
        public string GetTbColumnStr(string tbid)
        {
            if (!string.IsNullOrEmpty(tbid))
            {
                return comRepository.GetTbColumnStr(tbid);
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 表是否存在
        /// </summary>
        public bool TabExists(string tbName)
        {
            return comRepository.TabExists(tbName);
        }

        /// <summary>
        /// 根据录入表编码、指标编码、及类型生成数据库插入字段sql
        /// </summary>
        public string GetAddTbFieldSql(string tbid, string indexid, string dataType)
        {
            return comRepository.GetAddTbFieldSql(tbid, indexid, dataType);
        }

        /// <summary>
        /// 根据录入表编码、指标编码、及类型生成数据库修改字段sql
        /// </summary>
        public string GetAlterTbFieldSql(string tbid, string indexid, string dataType)
        {
            return comRepository.GetAlterTbFieldSql(tbid, indexid, dataType);
        }

        /// <summary>
        /// 根据设置的指标，生成数据表语句
        /// </summary>
        public string GetCreatDBStr(string tbid, string type, string fwId)
        {
            return comRepository.GetCreatDBSql(tbid, type, fwId);
        }

        /// <summary>
        /// 根据设置的指标，生成数据表语句
        /// </summary>
        public string GetCreatDBStr(string tbid)
        {
            return comRepository.GetCreatDBSqlForAux(tbid);
        }

        /// <summary>
        /// 执行sql语句
        /// </summary>
        public int ExecuteSql(string sql, object param = null)
        {
            return comRepository.ExecuteSql(sql, param);
        }

        /// <summary>
        /// 执行sql语句
        /// </summary>
        public DataTable ExecuteSql(List<string> sqlList, object param = null)
        {
            return comRepository.ExecuteSql(sqlList, param);
        }

        /// <summary>
        /// 执行sql语句，专用于执行After事件
        /// </summary>
        public List<string> ExecuteAfertEvent(ref string err, List<string> sqlList, object param, string listid, string _flowid, string flag)
        {
            try
            {
                return comRepository.ExecuteSqlList(sqlList, param);
            }
            catch (Exception ex)
            {
                err += flag + "出现错误" + ex.Message;
                Record.AddErr(listid.ToString(), _flowid, flag, ex);
                return null;
            }
        }

        /// <summary>
        /// 执行sql语句，专用于执行数据回写
        /// </summary>
        public List<string> ExecuteWriteBackData(ref string err, List<SqlAndParamEntity> list, string listid, string _flowid, string flag)
        {
            List<string> resultList = new List<string>();
            try
            {
                foreach (SqlAndParamEntity obj in list)
                {
                    comRepository.ExecuteSql(obj.Sql, obj.Param);
                    resultList.Add("#影响行数{" + comRepository.ExecuteSql(obj.Sql, obj.Param) + "}#" + "#Sql{" + obj.Sql + "} Parm" + JsonConvert.SerializeObject(obj.Param) + "#");
                }
                return resultList;
            }
            catch (Exception ex)
            {
                err += "" + ex.Message;
                Record.AddErr(listid.ToString(), _flowid, flag, ex);
                return null;
            }
        }

        /// <summary>
        /// 执行sql语句，专用于执行数据回写
        /// </summary>
        public void ExecuteWriteBackData(ref string err, string sql, object param)
        {
            try
            {
                comRepository.ExecuteSql(sql, param);
            }
            catch (Exception ex)
            {
                err += "错误:" + ex.Message;
            }
        }

        /// <summary>
        /// 生成自动编号数据建表语句
        /// </summary>
        public string GetCreatRuleAutoTbStr(string ruleId)
        {
            return comRepository.GetCreatRuleAutoTbSql(ruleId);
        }

        /// <summary>
        /// 返回DataTable
        /// </summary>
        public DataTable GetDataTable(string sql, object param = null)
        {
            return comRepository.GetDataTable(sql, param);
        }

        /// <summary>
        /// 返回DataTable
        /// </summary>
        public DataTable GetDataTable(string tbName, string some, string where, string order, object param = null)
        {
            string sql;
            if (string.IsNullOrEmpty(where))
            {
                sql = "SELECT  " + some + " FROM " + tbName + "  " + order;
            }
            else
            {
                string _where = where.ToLower().StartsWith("where ") ? where : "WHERE " + where;
                sql = "SELECT  " + some + " FROM " + tbName + "  " + _where + "  " + order;
            }
            return comRepository.GetDataTable(sql, param);
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        public long GetTotal(string tbName, string where = null, object param = null)
        {
            return comRepository.GetTotal(tbName, where, param);
        }
    }
}